package com.apkcore;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Readme {
    private static final String COM = "./src/com/apkcore/bl";
    private static final String HOST = "https://gitee.com/apkcore/Leetcode-bl/blob/master/src/com/apkcore/bl/";

    public static void main(String[] args) throws UnsupportedEncodingException {
        createReadme();
    }

    private static void createReadme() {
        try {

            StringBuilder content = new StringBuilder();
            content.append("LeetCode的一些题目解\n    小泊霖给我动力！\n\n");
            File aa = new File(COM);
            List<String> names = new ArrayList<>();
            if (aa.isDirectory()) {
                for (File file : Objects.requireNonNull(aa.listFiles())) {
                    names.add(file.getName());
                }
            }
            names.sort((o1, o2) -> {
//                String regEx = "[^0-9]";
//                Pattern p = Pattern.compile(regEx);
//                Matcher m1 = p.matcher(o1);
//                Matcher m2 = p.matcher(o2);

                try {
                    int a = Integer.parseInt(intStr(o1));
                    int b = Integer.parseInt(intStr(o2));
//                System.out.println( m.replaceAll("").trim());

//                String utf81 = str2UTF8(o1);
//                int index1 = utf81.indexOf("%");
//                String utf82 = str2UTF8(o2);
//                int index2 = utf82.indexOf("%");
//                    return Integer.parseInt(o1.substring(0, index1).replace("_", "")) - Integer.parseInt(o2.substring(0, index2).replace("_", ""));
                    return a - b;
                } catch (Exception e) {
                    return o1.compareTo(o2);
                }
            });
            boolean isFirstNoNum = true;
            for (String name : names) {
                String utf8 = str2UTF8(name);
                int index = intIndexOf(name);
                if (index > 0) {
                    String name1 = name.replace("_", "").substring(0, index);
                    String name2 = name.replace("_", "").substring(index);
                    if (name1.isEmpty()) {
                        if (isFirstNoNum) {
                            content.append("\n");
                            content.append("\n");
                            isFirstNoNum = false;
                        }
                        name = name2;
                    } else {
                        name = name1 + ". " + name2;
                    }
                }
                content.append("* [");
                content.append(name.replace(".java", "").replace("_", ""));
                content.append("](");
                content.append(HOST);
                content.append(utf8);
                content.append(")\n");
            }
            System.out.println(content);

            File file = new File("./README.MD");

            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content.toString());
            bw.close();
            System.out.println("Done");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 取字符串的前面数字
     */
    private static String intStr(String text) {
        char[] chars = text.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char aChar : chars) {
            if (aChar == '_') {
            } else if (aChar >= 48 && aChar <= 57) {
                sb.append(aChar);
            } else {
                break;
            }
        }
        return sb.toString();
    }

    /**
     * 字符串的数字位数
     */
    private static int intIndexOf(String text) {
        char[] chars = text.toCharArray();
        int index = -1;
        for (char aChar : chars) {
            if (aChar == '_') {
                index++;
            } else if (aChar >= 48 && aChar <= 57) {
                index++;
            } else {
                break;
            }
        }
        return index;
    }

    /**
     * 中文转utf8
     */
    private static String str2UTF8(String text) {
        try {
            return URLEncoder.encode(text, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * utf8转中文
     */
    private static String testUTF8ToStr(String utf8Str) {
        try {
            return URLDecoder.decode(utf8Str, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

}
